package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.kefu.MmochatKefuOptionLog;

public class MmochatGmOptionLogDao extends SimpleJdbcDaoSupport {
	private static final GmOptionLogRowMapper gmMapper = new GmOptionLogRowMapper();

	// 增加GM操作日志
	private static final String addGmOptionLogSql = "insert into "
			+ "kefu_option_log(kefu_id, role_id, role_name, type, content, reason, time)"
			+ " values(?, ?, ?, ?, ?, ?, ?)";

	public void addGmOptionLog(int kefu_id, int role_id, String role_name,
			int type, String content, String reason) {
		getSimpleJdbcTemplate().update(addGmOptionLogSql, kefu_id, role_id,
				role_name, type, content, reason,
				new Timestamp(System.currentTimeMillis()));
	}

	// 获取GM操作日志
	private static final String getGmOptionLogByIdSql = "select * from kefu_option_log "
			+ "where type = ? order by id desc limit ? offset ?";

	public List<MmochatKefuOptionLog> getGmOptionLogById(int type, int start,
			int count) {
		return getSimpleJdbcTemplate().query(getGmOptionLogByIdSql, gmMapper,
				type, count, start);
	}

	static class GmOptionLogRowMapper implements
			ParameterizedRowMapper<MmochatKefuOptionLog> {
		@Override
		public MmochatKefuOptionLog mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatKefuOptionLog log = new MmochatKefuOptionLog();
			log.setKefu_id(rs.getInt("kefu_id"));
			log.setTimestamp(rs.getTimestamp("time"));
			log.setRole_id(rs.getInt("role_id"));
			log.setRole_name(rs.getString("role_name"));
			log.setType(rs.getInt("type"));
			log.setContent(rs.getString("content"));
			log.setReason(rs.getString("reason"));
			return log;
		}
	}
}